const formatTime = date => {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();
    return [ year, month, day ].map(formatNumber).join("/") + " " + [ hour, minute, second ].map(formatNumber).join(":");
};

const formatNumber = n => {
    n = n.toString();
    return n[1] ? n : "0" + n;
}
/***
 * 判断用户滑动
 * 左滑还是右滑
 */;

const getTouchData = (endX, endY, startX, startY) => {
    let turn = "";
    if (endX - startX > 50 && Math.abs(endY - startY) < 50) {
        //右滑
        turn = "right";
    } else if (endX - startX < -50 && Math.abs(endY - startY) < 50) {
        //左滑
        turn = "left";
    }
    return turn;
}
/**
 * 版本号比较
 */;

function compareVersion(v1, v2) {
    v1 = v1.split(".");
    v2 = v2.split(".");
    const len = Math.max(v1.length, v2.length);
    while (v1.length < len) {
        v1.push("0");
    }
    while (v2.length < len) {
        v2.push("0");
    }
    for (let i = 0; i < len; i++) {
        const num1 = parseInt(v1[i]);
        const num2 = parseInt(v2[i]);
        if (num1 > num2) {
            return 1;
        } else if (num1 < num2) {
            return -1;
        }
    }
    return 0;
}

// 将百度地图经纬度转换为腾讯/高德地图经纬度
function bMapTransQQMap(lng, lat) {
    let x_pi = 3.141592653589793 * 3e3 / 180;
    let x = lng - .0065;
    let y = lat - .006;
    let z = Math.sqrt(x * x + y * y) - 2e-5 * Math.sin(y * x_pi);
    let theta = Math.atan2(y, x) - 3e-6 * Math.cos(x * x_pi);
    let lngs = z * Math.cos(theta);
    let lats = z * Math.sin(theta);
    return {
        lng: lngs,
        lat: lats
    };
}

function showLoading(message) {
    if (wx.showLoading) {
        // 基础库 1.1.0 微信6.5.6版本开始支持，低版本需做兼容处理
        wx.showLoading({
            title: message,
            mask: true
        });
    } else {
        // 低版本采用Toast兼容处理并将时间设为20秒以免自动消失
        wx.showToast({
            title: message,
            icon: "loading",
            mask: true,
            duration: 2e4
        });
    }
}

function hideLoading() {
    if (wx.hideLoading) {
        // 基础库 1.1.0 微信6.5.6版本开始支持，低版本需做兼容处理
        wx.hideLoading();
    } else {
        wx.hideToast();
    }
}

function numberFormatter(num, point) {
    let numStr = Number(num).toFixed(0).toString();
    // 十万以内直接返回 
        if (numStr.length < 6) {
        return num;
    }
    //大于8位数是亿
     else if (numStr.length > 8) {
        let decimal = numStr.substring(numStr.length - 8, numStr.length - 8 + point);
        return parseFloat(parseInt(num / 1e8) + "." + decimal) + "亿";
    }
    //大于6位数是十万 (以10W分割 10W以下全部显示)
     else if (numStr.length > 5) {
        let decimal = numStr.substring(numStr.length - 4, numStr.length - 4 + point);
        return parseFloat(parseInt(num / 1e4) + "." + decimal) + "万";
    }
}

module.exports = {
    formatTime: formatTime,
    getTouchData: getTouchData,
    compareVersion: compareVersion,
    bMapTransQQMap: bMapTransQQMap,
    showLoading: showLoading,
    hideLoading: hideLoading,
    numberFormatter: numberFormatter
};